import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;

public class BackgroundDraw extends Superclass implements WindowListener
{
	private Robot controlMouse;
	public boolean endLoop;
	
	public BackgroundDraw() throws AWTException
	{
		this.setSize(300, 300);
		endLoop = true;
		super.path = "test image.jpg";
		super.xCoord = 100;
		super.yCoord = 100;
		imgExtractSize[0] = this.getWidth();
		imgExtractSize[1] = this.getHeight();
		this.setVisible(true);
		this.addKeyListener(this);
		this.addWindowListener(this);
		this.addMouseWheelListener(this);
		controlMouse = new Robot();
		watchMouse();
	}

	public void keyPressed(KeyEvent k)
	{
		int c = k.getKeyCode();
		if(c == k.VK_UP && super.yCoord != 0)
		{
			super.yCoord -= 1;
		}	
		else if (c == k.VK_DOWN && super.yCoord < super.totalImgSize[1] - super.imgExtractSize[1])
		{
			super.yCoord += 1;
		}
		else if (c == k.VK_LEFT && super.xCoord != 0)
		{
			super.xCoord -= 1;
		}
		
		else if (c == k.VK_RIGHT && super.xCoord < super.totalImgSize[0] - super.imgExtractSize[0])
		{
			super.xCoord += 1;
		}
	}
	public void mouseWheelMoved(MouseWheelEvent e) 
	{
		//getWheelRotation, a method of MouseWheelEvent, is a way to tell the 
		//direction of the mouse wheel scroll (<0 is Up and > 0 is down)
		if (e.getWheelRotation() > 0 && super.scaleFactor > .75)
				super.scaleFactor -= .0050;
		else if (super.scaleFactor < 1.25 && e.getWheelRotation() < 0)
			super.scaleFactor += .0050;
	}
	
	public void watchMouse()
	{
		Point p = this.getLocation();
		Point mousePosition = MouseInfo.getPointerInfo().getLocation();
			
		if (mousePosition.x <= p.x + 15 && mousePosition.y <= p.y + 40 && xCoord != 0 && yCoord != 0)
		{
			xCoord -= 1;
			yCoord -= 1;
			sleep();
		}
		else if (mousePosition.x <= p.x + this.getWidth()  && mousePosition.x >= p.x + this.getWidth() - 15
				&& mousePosition.y <= p.y + 40 && xCoord != totalImgSize[0] -  imgExtractSize[0] && yCoord != 0)
		{
			xCoord += 1;
			yCoord -= 1;
			sleep();
		}
		else if (mousePosition.x <= p.x + 15 && mousePosition.y <= p.y + this.getHeight()&& mousePosition.y >= p.y + this.getHeight() - 15
				&& xCoord != 0 && yCoord < totalImgSize[1] - imgExtractSize[1])
		{
			xCoord -= 1;
			yCoord += 1;
			sleep();
		}
		else if ((mousePosition.x <= p.x + this.getWidth() && mousePosition.x >= p.x + this.getWidth() - 15) && (mousePosition.y <= p.y + this.getHeight() && 
				mousePosition.y >= p.y + this.getHeight() - 15) && xCoord != totalImgSize[0] -  imgExtractSize[0] && yCoord != totalImgSize[1] -  imgExtractSize[1])
		{
			xCoord += 1;
			yCoord += 1;
			sleep();
		}
		else
		{
			if ((mousePosition.x >= p.x && mousePosition.x < p.x + 15) && super.xCoord != 0 && 
					(mousePosition.y > p.y && mousePosition.y < this.getHeight() + p.y))
			{
				super.xCoord -= 1;
				sleep();
			} 
			else if (mousePosition.x > p.x+this.getWidth() - 15 && mousePosition.x <= p.x+this.getWidth() 
					&& super.xCoord < super.totalImgSize[0] - super.imgExtractSize[0] && ((mousePosition.y > 0 
					+ this.getLocation().y && mousePosition.y < this.getHeight() + p.y)))
			{
				super.xCoord += 1;
				sleep();
			}
			else if ((mousePosition.y > p.y + 25 && mousePosition.y < p.y + 40) && super.yCoord != 0
					&& (mousePosition.x > p.x && mousePosition.x < this.getWidth() + p.x))
			{
				super.yCoord -= 1;
				sleep();
			}
			else if ((mousePosition.y >= p.y + this.getHeight() - 15 && mousePosition.y < p.y + this.getHeight()) 
					&& super.yCoord != super.totalImgSize[1] - super.imgExtractSize[1] && (mousePosition.x > p.x && mousePosition.x < this.getWidth() + p.x))
			{
				super.yCoord += 1;
				sleep();
			}
		}
}
	
	public void paint(Graphics g)
	{
		BufferedImage i = extractImage();
		
		g.drawImage(i, 0, 0, imgExtractSize[0], imgExtractSize[1], null);
	}
	
	public void sleep()
	{
		try {
			Thread.currentThread().sleep(10);
		} catch (InterruptedException e) {}
	}
	
	public void keyReleased(KeyEvent arg0) {}
	public void keyTyped(KeyEvent k)	{}
	public void windowActivated(WindowEvent arg0) {}
	public void windowClosed(WindowEvent arg0) {}
	public void windowClosing(WindowEvent arg0) 
	{
		endLoop = false;
	}
	public void windowDeactivated(WindowEvent arg0) {	}
	public void windowDeiconified(WindowEvent arg0) {}
	public void windowIconified(WindowEvent arg0) {}
	public void windowOpened(WindowEvent arg0) {}
}